//
//  ChartsViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/5.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "ChartsViewController.h"
#import "AFNetworking.h"
#import "NSString+Html.h"

@interface ChartsViewController () <UIWebViewDelegate>
{
    MBProgressHUD *hud;
}
@property (nonatomic,retain)AFHTTPRequestOperation *operation; // 操作。
@property (nonatomic,retain) NSOperationQueue *queue;  // 操作队列
@property (nonatomic,retain)NSString *htmlStr;
@property (nonatomic,retain)UIWebView *webView;

@end


@implementation ChartsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createdownLoadProgress];
    [self requestData];
}

-(void)requestData{
    NSString *str = [NSString stringWithFormat:@"http://api.m.mtime.cn/Review/Detail.api?reviewId=%@",self.reviewId];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        self.htmlStr =  dic[@"content"];
        [self createWebView];
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue = queue;
    [queue release];

}


//创建webView
-(void)createWebView{
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    self.webView.y = 0;
    _webView.delegate = self;
    NSString *newStr = [NSString importStyleWithHtmlString:self.htmlStr];
    [self.webView loadHTMLString:newStr baseURL:[NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath]];
    [self.view addSubview:self.webView];
    [_webView release];
   }


// 加载进度条
- (void)createdownLoadProgress{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.color = [UIColor grayColor];
    [self.view addSubview:hud];
    hud.labelText = @"努力加载中..";
    //设置模式为环状进度提示框
    [hud show:YES];
    [hud release];
}


- (void)webViewDidStartLoad:(UIWebView *)webView{
    
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    if (hud) {
        [hud hide:YES];
        [hud removeFromSuperview];
    }
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSMutableString *str = [NSMutableString string];
    [str appendString:@"var arr = document.getElementsByTagName('img');"];
    NSString *str2 = [NSString stringWithFormat:@"for(var i=0;i<arr.length;i++){arr[i].marginLeft = 100;arr[i].width = %f;}",kScreen_width -15];
    [str appendString:str2];
    [webView stringByEvaluatingJavaScriptFromString:str];

}




- (void)dealloc{
    [_webView release];
    [_queue release];
    [_operation release];
    [_reviewId release];
    [_htmlStr release];
    [super dealloc];
}

@end
